﻿using System.Collections.Generic;
using UnityEngine;

namespace Framework
{
    /// <summary>
    /// 用来保存sprite集合
    /// </summary>
    [CreateAssetMenu(fileName = "newspriteset.asset", menuName = "SpriteSet", order = 1)]
    public class SpriteSet : ScriptableObject
    {
        [SerializeField]
        public List<Sprite> contentSet;

        [LuaInterface.NoToLua]
        [Header("拖拽目标文件夹")]
        public UnityEngine.Object folder;

        private Dictionary<string, Sprite> _spriteSetDict;

        public Sprite GetSprite(string spriteName)
        {
            if (null == _spriteSetDict)
            {
                _spriteSetDict = new Dictionary<string, Sprite>();
                foreach (var sprite in contentSet)
                {
                    var spName = sprite.name;
                    if (!_spriteSetDict.ContainsKey(spName))
                    {
                        _spriteSetDict.Add(spName, sprite);
                    }
                    else
                    {
                        Logger.LogError($"SpriteSet {name} sprite name duplicate: {spName}");
                    }
                }
            }
            if (_spriteSetDict.TryGetValue(spriteName, out Sprite ret))
            {
                return ret;
            }
            Logger.LogError($"can not get spriteName = {spriteName} in SpriteSet = {name}");
            return null;
        }

        public void Clear()
        {
            _spriteSetDict = null;
            if (null == contentSet)
                return;
            foreach (var item in contentSet)
            {
                Destroy(item);
            }
            contentSet.Clear();
        }
    }
}